package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Address;
import com.itheima.pojo.CheckItem;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Address> page = addressDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Address> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public List<Address> findAll() {
        return addressDao.findAll();

    }

    @Override
    public void add(Address add) {
        addressDao.add(add);
    }

    @Override
    public void del(Integer id) {
        long count = addressDao.findCountById(id);
        if(count>0){
            throw new RuntimeException();
        }
        addressDao.del(id);
    }
}
